package com.mjk.common.web.getway.restful.prototal.http;

import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.servlet.http.HttpSession;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.context.request.NativeWebRequest;
import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.context.request.ServletWebRequest;
import org.springframework.web.servlet.HandlerMapping;
import org.springframework.web.servlet.ModelAndView;

import java.util.Map;
/**
 * contoller 基类
 * @author mjk
 *
 */
public class BaseController {
	public static final String SYS_USER_SESSION_NAME = "userInfo";

	public static final String CLINT_USER_SESSION_NAME = "clientuserInfo";
	/**
	 * 文件切割大小
	 */
	public static final long SPLIX_FILE_SIZE = 5*1024*1024L;
	
	public static final String VERSION = "version";

    @SuppressWarnings("unchecked")
	@ModelAttribute
	public void getModel(HttpServletRequest request, HttpServletResponse response) {
        HttpSession session = request.getSession();
        String ip = ((String) request.getSession().getAttribute("ip"));
		NativeWebRequest webRequest = new ServletWebRequest(request);
		Map<String, String> map = 
				(Map<String, String>) webRequest.getAttribute(HandlerMapping.URI_TEMPLATE_VARIABLES_ATTRIBUTE, RequestAttributes.SCOPE_REQUEST);

        String version = null!=map && map.containsKey(VERSION) ? map.get(VERSION) : "v1";
	}
	
	public ModelAndView getModelAndView(String viewName, Map<String, Object> args) {
		ModelAndView mv = new ModelAndView();
		mv.setViewName(viewName);
		if (args != null) {
			for (Map.Entry<String, Object> entry : args.entrySet()) {
				mv.addObject(entry.getKey(), entry.getValue());
			}
		}
		return mv;
	}
}